﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32; 

namespace Wince6_app2 {
	public static class RegistryHandle {

		/// <summary>
		/// store a key value in registry. if it don't exist it will be created. 
		/// </summary>
		/// <param name="mainKey">the main key of key path</param>
		/// <param name="subKey">the path below the main key</param>
		/// <param name="keyName">the key name</param>
		/// <param name="value">the value to be stored</param>
		public static void SetRegistry( RegistryKey mainKey, String subKey, String keyName, object value ) {
			if ( mainKey != Registry.CurrentUser && mainKey != Registry.LocalMachine ) {
				throw new ArgumentOutOfRangeException( "mainKey", "\'mainKey\' argument can only be AppUtils.CURRENT_USER or AppUtils.LOCAL_MACHINE values" );
			}

			if ( subKey == null ) {
				throw new ArgumentNullException( "subKey", "\'subKey\' argument cannot be null" );
			}

			if ( keyName == null ) {
				throw new ArgumentNullException( "keyName", "\'keyName\' argument cannot be null" );
			}

			const Boolean WRITABLE = true;
			RegistryKey key = null;

			if ( mainKey == Registry.CurrentUser ) {
				key = Registry.CurrentUser.OpenSubKey( subKey, WRITABLE );

				if ( key == null ) {
					key = Registry.CurrentUser.CreateSubKey( subKey );
				}
			}
			else if ( mainKey == Registry.LocalMachine ) {
				key = Registry.LocalMachine.OpenSubKey( subKey, WRITABLE );

				if ( key == null ) {
					key = Registry.LocalMachine.CreateSubKey( subKey );
				}
			}

			key.SetValue( keyName, value );
			key.Flush();
		}

		/// <summary>
		/// find a key value in registry. if it don't exist the default value will be returned.
		/// </summary>
		/// <param name="mainKey">the main key of key path</param>
		/// <param name="subKey">the path below the main key</param>
		/// <param name="keyName">the key name</param>
		/// <param name="defaultValue">the value to be stored</param>
		public static object GetRegistry( RegistryKey mainKey, String subKey, String keyName, object defaultValue ) {
			if ( mainKey != Registry.CurrentUser && mainKey != Registry.LocalMachine ) {
				throw new ArgumentOutOfRangeException( "mainKey", "\'mainKey\' argument can only be AppUtils.CURRENT_USER or AppUtils.LOCAL_MACHINE values" );
			}

			if ( subKey == null ) {
				throw new ArgumentNullException( "subKey", "\'subKey\' argument cannot be null" );
			}

			if ( keyName == null ) {
				throw new ArgumentNullException( "keyName", "\'keyName\' argument cannot be null" );
			}

			RegistryKey key = Registry.CurrentUser.OpenSubKey( subKey );

			if ( mainKey == Registry.CurrentUser ) {
				key = Registry.CurrentUser.OpenSubKey( subKey );
			}
			else if ( mainKey == Registry.LocalMachine ) {
				key = Registry.LocalMachine.OpenSubKey( subKey );
			}

			object result = defaultValue;

			if ( key != null ) {
				result = key.GetValue( keyName, defaultValue );
			}

			return result;
		}

	}
}
